package com.shenganyuan.smac.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.oklib.base.CoreBaseFragment;
import com.oklib.widget.dialog.ConfirmDialog;
import com.shenganyuan.smac.App;
import com.shenganyuan.smac.R;
import com.shenganyuan.smac.activity.ActAboutMe;
import com.shenganyuan.smac.common.beans.ApiMainUser;
import com.shenganyuan.smac.leave.ActLeave;
import com.shenganyuan.smac.task.ActTasks;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Damon
 * @date 2017/12/14
 */

public class FrgMainMe extends CoreBaseFragment {
    @BindView(R.id.work_user_username)
    TextView work_user_username;
    @BindView(R.id.work_user_cylb)
    TextView work_user_cylb;
    @BindView(R.id.work_user_department)
    TextView work_user_department;
    @BindView(R.id.work_user_zhiwu)
    TextView work_user_zhiwu;
    @BindView(R.id.btn_logout)
    TextView btnLogout;

    public static FrgMainMe newInstance() {
        Bundle args = new Bundle();
        FrgMainMe fragment = new FrgMainMe();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.main_frg_me;
    }

    @Override
    public void initUI(View view, @Nullable Bundle savedInstanceState) {
    }

    @Override
    public void initData() {
        super.initData();
        getUserData();
    }

    private void getUserData() {
        ApiMainUser mainUser = App.getApiMainUser();
        if (mainUser != null) {
            work_user_username.setText(mainUser.name);
            work_user_cylb.setText(mainUser.cylb);
            work_user_department.setText(mainUser.bumen);
            work_user_zhiwu.setText(mainUser.zhiwu);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.btn_logout, R.id.main_me_leave_container, R.id.main_me_tasks, R.id.main_me_about_container})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_logout:
                logout();
                break;
            case R.id.main_me_leave_container:
                startActivity(new Intent(mContext, ActLeave.class));
                break;
            case R.id.main_me_about_container:
                startActivity(new Intent(mContext, ActAboutMe.class));
                break;
            case R.id.main_me_tasks:
                startActivity(new Intent(mContext, ActTasks.class));
                break;
        }

    }

    /**
     * 退出登录
     */
    private void logout() {
        ConfirmDialog confirmDialog = new ConfirmDialog(mContext);
        confirmDialog.setMessage("即将退出登录，请确认!").setPositiveButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //清理动作
                App.loginOut(getContext());
                confirmDialog.dismiss();
                getActivity().finish();
            }
        }).setNevigateButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmDialog.dismiss();
            }
        }).show();
    }
}
